package com.emp.test.springboottest.domain.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.emp.test.springboottest.service.YhdService;
import com.yhd.object.product.Product;

@Repository(value = "productDao")
public class ProductDao {
	
	final static Logger logger = LoggerFactory.getLogger(ProductDao.class);
	
	@Autowired
	NamedParameterJdbcTemplate namedParameterJdbcTemplate;
	
	@Autowired
	JdbcTemplate jdbcTemplate;
	
	public ProductDao() {
		super();
	}

	@Transactional
	public int[] batchInsert(List<Product> product, String ownerId){
		if(null == product) return null;

		SqlParameterSource[] batch = SqlParameterSourceUtils.createBatch(product.toArray());
        int[] updateCounts = namedParameterJdbcTemplate.batchUpdate(
                "REPLACE into yhd_product(productId,uptime,ownerId) values(:productId,'"+ DateFormatUtils.ISO_DATETIME_FORMAT.format(new Date()) + "', '" + ownerId + "' )",
                batch);
        
        String[] infomsg = {"异常","新增","更新"};
        for(int i = 0; i < updateCounts.length; i++){
        	if(updateCounts[i] > 2 || 0 == updateCounts[i]){
        		logger.error(infomsg[updateCounts[i]] + JSON.toJSONString(product.get(i)));
        	}else{
        		logger.info(infomsg[updateCounts[i]]);
        	}
        }
        
        return updateCounts;
	}
	
	@Transactional
	public void insert(Product product){
		
	}
	
	public List<Product> getAllProduct(String ownerId){
		if(ownerId == null || ownerId.isEmpty()) return new ArrayList<>();
		
		List<Product> productList = this.jdbcTemplate.query(
		        " SELECT productId from yhd_product where ownerId = '" +ownerId  + "' and productId is not null",
		        new RowMapper<Product>() {
		            public Product mapRow(ResultSet rs, int rowNum) throws SQLException {
		            	Product product = new Product();
		            	product.setProductId(rs.getLong("productId"));
		                return product;
		            }
		        });
		
		return productList;
	}
	
	/**
	 * 获取一个用户全部的产品id
	 * @param ownerId
	 * @return
	 */
	public String getAllid(String ownerId){
		if(ownerId == null || ownerId.isEmpty()) return null;
		String idConcats;
		idConcats = jdbcTemplate.queryForObject("SELECT GROUP_CONCAT(productId) from yhd_product where ownerId = '" + ownerId  + "' and  productId is not null", String.class);
		logger.info("ownerId"  + ownerId + "all product idConcats " + idConcats);
		return idConcats;
	}
	
}
